package com.yuan.serviceribbon.service.impl;/**
 * @ClassName HelloServiceImpl
 * @Description TODO
 * @Author yuan
 * @Date 2020/8/4 11:36
 * @Version 1.0
 */

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yuan.serviceribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.logging.Logger;

/**
 * @ClassName HelloServiceImpl
 * @Description:
 * @Author yuan
 * @Date 2020/8/4 
 * @Version V1.0
 **/
@Service
@RefreshScope  //实现配置的热加载
public class HelloServiceImpl implements HelloService {

    private static final Logger logger = Logger.getLogger(HelloServiceImpl.class.getName());

    private static final String SERVICE = "http://SERVICE-HELLO/";

    @Autowired
    private RestTemplate restTemplate;

    @Value("${username:yuan}")
    private String username;

    @Override
    public String hello(String name) {
        return "this is nacos test -->" + username;
//        return restTemplate.getForObject(SERVICE + "hello?name=" + name ,String.class);
    }

    @HystrixCommand(fallbackMethod = "hystrixError")
    @Override
    public String hystrixHi(String name) {
        return "nacos config:" + username + "<br/>" +
                restTemplate.getForObject(SERVICE + "hello" ,String.class);
    }

    public String hystrixError(String name){
        return "hello ribbon, hystrix error！ -->" +name ;
    }
}
